package dao;

import dao.Entity.MyTime;
import dao.Entity.PlaceSchedule;
import dao.Entity.PlaceTime;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;

public class QueryPlaceSchedule {

    public static PlaceSchedule getResult(String placeNumber) {
        MyConnection myCon = new MyConnection();
        myCon.getConnection();
        PlaceSchedule placeSchedule = null;
        HashMap<String, Integer> map = new HashMap<>();
        try {
            PreparedStatement ps = myCon.connection.prepareStatement(
                    "select * from placetime where PlaceNumber=? order by placedate ASC");
            ps.setString(1, placeNumber);
            ResultSet rs = ps.executeQuery();
            placeSchedule = new PlaceSchedule();
            PlaceTime placeTime = null;
            int index = 0;
            while (rs.next()) {
                String placeDate = rs.getString(3);
                if (!map.containsKey(placeDate)) {
                    map.put(placeDate, index++);
                    placeSchedule.appendSchedule(new PlaceTime(placeDate));
                }
                placeTime = placeSchedule.Schedule.get(map.get(placeDate));
                placeTime.appendTime(new MyTime(rs.getString(1), rs.getString(4), rs.getString(5), rs.getBoolean(6)));
            }
            ps = myCon.connection.prepareStatement(
                    "select PlacePosition from place where PlaceNumber=?");
            ps.setString(1, placeNumber);
            rs = ps.executeQuery();
            while (rs.next()) {
                placeSchedule.setName(rs.getString(1));
            }
            ps.close();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            myCon.closeConnection();
        }
        return placeSchedule;
    }
}
